package com.hdoit.kylin.logical.studentManager.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.kylin.logical.studentManager.dao.IMidCheckDao;
import com.hdoit.kylin.logical.studentManager.model.MidCheck;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.viewmodel.postgraduate.MidCheckSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;

/**
 * @author anshengqiang
 * @email
 * @createtime 2017年07月26日 11:16:39
 * @comment
 */
@Component
public class MidCheckDaoImpl extends SuperDao<MidCheck> implements IMidCheckDao {

	@Override
	public int queryCount(MidCheckSearch search) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT midCheck.id) as count")
				.from(MidCheck.class, "midCheck")
				.leftJoin(Student.class, "student")
				.on("midCheck.checkedId", "student.employeeId")
				.andWhere("student.employeeId IS NOT NULL")
				.leftJoin(Teacher.class, "teacher")
				.on("midCheck.checkId", "teacher.employeeId")
				.setMainTableAlias("midCheck");
		return queryCount(finderExpression, search);
	}

	@Override
	public List<MidCheck> queryList(MidCheckSearch search, PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("midCheck.*,student.studentName as studentName,teacher.teacherName as teacherName")
									 .from(MidCheck.class,"midCheck")
									 .leftJoin(Student.class,"student")
									 .on("midCheck.checkedId","student.employeeId")
									 .andWhere("student.employeeId IS NOT NULL")
									 .leftJoin(Teacher.class,"teacher")
									 .on("midCheck.checkId","teacher.employeeId")
									 .addOrder("checkDate", OrderEnum.desc)
									 .setMainTableAlias("checkId");
		if(pagerModel==null){
			return queryList(finderExpression,search); 
		}
		return queryList(finderExpression,search,pagerModel);
	}
}